package com.atmc.controller;

import com.atmc.entity.Files;
import com.atmc.entity.ResponseResult;
import com.atmc.entity.User;
import com.atmc.service.FilesService;
import com.atmc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@CrossOrigin
@Slf4j
@RestController
public class MinController{
    @Autowired
    private StreamBridge streamBridge;
    @Autowired
    UserService userService;
    @Autowired
    FilesService filesService;
    @RequestMapping("/info")
    public String hello(){
        return "hello SpringSecurity. chuyings";
    }
    @PostMapping("/upfile")
    public ResponseResult upfiles(@RequestPart("photos") MultipartFile[] photos) throws IOException {
        for (MultipartFile photo : photos) {
            String originalFilename = photo.getOriginalFilename();

            FileInputStream fis = (FileInputStream) photo.getInputStream();
        }
        return new ResponseResult(200,"上传成功");
    }
    @PostMapping("/loadfile")
    public ResponseResult loadfile(){
        return new ResponseResult();
    }

    @GetMapping("/test")
    public ResponseResult test(){
//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        int a=2/0;
        ResponseResult result=new ResponseResult();
        result.setCode(200);
        result.setData("调试");
        result.setData("返回数据");
        return result;
    }
    @PostMapping("/user-selectOne/{userName}")
    public User selectOne(@PathVariable("userName")String userName){
        User user = userService.selectOne(userName);
        return user;
    }
    @RequestMapping(value = "/userById/{user_id}",method = RequestMethod.GET)
    public User userselectById(@PathVariable("user_id")Long user_id) {
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userService.selectOneById(user_id);
    }


    @PostMapping("/sendMessage/{userName}")
    public String sendMessage(@PathVariable("userName")String message){
        streamBridge.send("output",message);
        return message;
    }
//    @PostMapping("/sendMessage")
//    public User sendMessage2(@RequestBody User user){
//        streamBridge.send("output",user);
//        return user;
//    }
    @PostMapping("/sendMessage")
    public ResponseResult sendMessage2(@RequestParam("userName")String userName,@RequestParam("password")String password){
        streamBridge.send("gather1",userName);
        streamBridge.send("gather2",password);
        return new ResponseResult(200,"发送成功");
    }

}